﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Common;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Utility;
using SharpDiary.Reminder;

namespace SharpDiary.Task
{
    [GuiModule("d53dfd48-fb6f-408f-91d8-009f79e84c09", "ReminderEditor", "Ray Shaw(demogodyou@gmail.com)")]
    public partial class frmReminderEditor : GuiModuleUIBase
    {
        #region Fields & Properties

        private ReminderInfo _reminder;
        private ReminderInfoListItemControl _item;

        public ReminderInfo Reminder
        {
            get { return _reminder; }
            set
            {
                _reminder = value;
                DisplayReminderInfo();
            }
        }

        #endregion

        #region Constructors

        public frmReminderEditor()
        {
            InitializeComponent();

            UpdateUI();
        }

        #endregion

        #region Methods

        /// <summary>
        /// 更新界面。
        /// </summary>
        private void UpdateUI()
        {
            reminderDateTimePicker.Value = DateTime.Now;
        }

        /// <summary>
        /// 显示提醒信息。
        /// </summary>
        private void DisplayReminderInfo()
        {
            if (_reminder == null)
            {
                txtName.Clear();
                txtDescription.Clear();
                reminderDateTimePicker.Value = DateTime.Now;
                reminderDateTimePicker.ReminderType = ReminderType.DateAndTime;

                return;
            }

            txtName.Text = _reminder.Name;
            txtDescription.Text = _reminder.Detail;
            reminderDateTimePicker.Value = _reminder.AlarmDatetime;
            reminderDateTimePicker.ReminderType = (ReminderType)_reminder.ReminderType;
        }

        public override void SetParameter(object parameter, object sender)
        {
            base.SetParameter(parameter, sender);

            ReminderInfo reminderInfo = parameter as ReminderInfo;
            _reminder = reminderInfo;
            _item = sender as ReminderInfoListItemControl;

            DisplayReminderInfo();
        }

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        /// <summary>
        /// 检查输入有效性。
        /// </summary>
        /// <returns></returns>
        private bool CheckIfInputOk()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                ShowMessage("请您输入提醒名称。");
                txtName.Focus();
                txtName.SelectAll();

                return false;
            }

            DateTime temp = reminderDateTimePicker.Value;
            DateTime datetime = new DateTime(temp.Year, temp.Month,
                   temp.Day, temp.Hour, temp.Minute,
                    0);

            if (reminderDateTimePicker.ReminderType == ReminderType.DateAndTime && datetime <= DateTime.Now)
            {
                ShowMessage("请设置的提醒时间已过期，请设置为将来的时间。");
                reminderDateTimePicker.Focus();

                return false;
            }

            return true;
        }

        /// <summary>
        /// 保存。
        /// </summary>
        private void Save()
        {
            if (!CheckIfInputOk()) { return; }

            if (_reminder == null || string.IsNullOrEmpty(_reminder.Name))
            {
                _reminder = new ReminderInfo();
                _reminder.Name = txtName.Text;
                _reminder.Detail = txtDescription.Text;
                _reminder.ReminderType = (int)reminderDateTimePicker.ReminderType;
                DateTime alarmDatetime = reminderDateTimePicker.Value;
                _reminder.AlarmDatetime = new DateTime(alarmDatetime.Year, alarmDatetime.Month,
                   alarmDatetime.Day, alarmDatetime.Hour, alarmDatetime.Minute, 0);

                if (ReminderInfo.Create<ReminderInfo>(_reminder))
                {
                    OwnerShell.NotifyMessage(this, new MessageReceivedEventArgs(MessageDefinition.Refresh,
                        "d53dfd48-fb6f-408f-91d8-009f79e84c09", "2ec3064d-e03b-43f8-bf94-b139e49c6380"));

                    ShowMessage("保存成功。");
                    if (_item != null)
                    {
                        _item.Reminder = _reminder;
                    }

                    DelayClose();
                }
                else
                {
                    ShowMessage("保存失败。");
                }
            }
            else
            {
                _reminder.Name = txtName.Text;
                _reminder.Detail = txtDescription.Text;
                _reminder.ReminderType = (int)reminderDateTimePicker.ReminderType;
                DateTime alarmDatetime = reminderDateTimePicker.Value;
                _reminder.AlarmDatetime = new DateTime(alarmDatetime.Year, alarmDatetime.Month,
                   alarmDatetime.Day, alarmDatetime.Hour, alarmDatetime.Minute, 0);

                if (ReminderInfo.Update<ReminderInfo>(_reminder))
                {
                    ShowMessage("更新成功。");
                    if (_item != null)
                    {
                        _item.Reminder = _reminder;
                    }

                    DelayClose(1000);
                }
                else
                {
                    ShowMessage("保存失败。");
                }
            }
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnManageTasks_Click(object sender, EventArgs e)
        {
            OwnerShell.ExecuteAction("SharpDiary.Reminder", "2ec3064d-e03b-43f8-bf94-b139e49c6380", _reminder, this);
        }
    }
}
